package com.hsw.habit.controller;

import com.hsw.habit.frame.annotation.CurrentUser;
import com.hsw.habit.model.JsonData;
import com.hsw.habit.model.NoteGroups;
import com.hsw.habit.model.User;
import com.hsw.habit.model.bean.ListResponse;
import com.hsw.habit.service.GroupService;
import com.hsw.habit.service.NoteGroupService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/note/group")
public class NoteGroupController {

    @Resource
    NoteGroupService groupService;

    @RequestMapping("/index")
    public JsonData index(@CurrentUser User user) {
        ListResponse<NoteGroups> listResponse = new ListResponse<>();
        listResponse.setList(groupService.selectGroupsByUserId(user.getId()));
        return JsonData.success(listResponse);
    }

    @RequestMapping("/add")
    public JsonData add(@CurrentUser User user,NoteGroups noteGroups) {
        noteGroups.setUserId(user.getId());
        boolean res = groupService.insertGroup(noteGroups);
        if (!res) {
            return JsonData.error();
        }
        return JsonData.success();
    }

    @RequestMapping("/update")
    public JsonData update(NoteGroups noteGroups) {
        if (null == noteGroups) {
            return JsonData.error();
        }

        NoteGroups groups = groupService.selectGroupById(noteGroups.getId());

        if (null != groups) {
            groups.setGroupName(noteGroups.getGroupName());
        }

        boolean res = groupService.updateGroup(groups);
        if (!res) {
            return JsonData.error();
        }
        return JsonData.success();
    }

    @RequestMapping("/remove")
    public JsonData remove(int id) {
        if (id <= 0) {
            return JsonData.error();
        }
        boolean res = groupService.deleteGroup(id);
        if (!res) {
            return JsonData.error();
        }
        return JsonData.success();
    }
}
